import nedb from './nedb'

export default {
  /**
   * 添加用户登录
   * @param {Object} user 用户信息
   */
  async addUser (user) {
    try {
      var result = await nedb.user_list.find({butt_id: user.butt_id, user_id: user.user_id})
      if (result.length > 0) { // 已经存在了更新数据
        await nedb.user_list.update({butt_id: user.butt_id, user_id: user.user_id}, user)
      } else { // 不存在添加数据
        await nedb.user_list.insert(user)
      }
      return Promise.resolve(user)
    } catch (e) {
      return Promise.resolve(null)
    }
  },
  /**
   * 获取用户
   * @param {Number} id 客服ID
   */
  async getUser (id) {
    try {
      var result = await nedb.user_list.find({butt_id: id})
      return Promise.resolve(result)
    } catch (e) {
      return Promise.resolve([])
    }
  }
}
